package spe.jbpm.gd.jpdl.model;

import java.util.ArrayList;
import java.util.List;

import spe.model.common.SemanticElementImpl;

public class AbstractNode extends SemanticElementImpl implements NodeElement, DescribableElement 
{

	private List events = new ArrayList();
	private List exceptionHandlers = new ArrayList();
	private List transitions = new ArrayList();
	private Description description;
	private DocumentState documentState;
	
	public void setDocumentState(DocumentState newDocumentState) 
	{
		DocumentState oldDocumentState = documentState;
		documentState = newDocumentState;
		//firePropertyChange("document-state", oldDocumentState, newDocumentState);
		makePropertyChangedNotify(this, "document-state", oldDocumentState, newDocumentState);
	}
	
	public DocumentState getDocumentState() {
		return documentState;
	}	
	
	public void setDescription(Description newDescription) {
		Description oldDescription = description;
		description = newDescription;
		//firePropertyChange("description", oldDescription, newDescription);
		makePropertyChangedNotify(this, "description", oldDescription, newDescription);
	}
	
	public Description getDescription() {
		return description;
	}
	
	public void addEvent(Event event) {
		events.add(event);
		//firePropertyChange("eventAdd", null, event);
		makePropertyChangedNotify(this, "eventAdd", null, event);
	}
	
	public void removeEvent(Event event) {
		events.remove(event);
		makePropertyChangedNotify(this, "eventRemove", event, null);
	}
	
	public Event[] getEvents() {
		return (Event[])events.toArray(new Event[events.size()]);
	}
	
	public void addExceptionHandler(ExceptionHandler exceptionHandler) {
		exceptionHandlers.add(exceptionHandler);
		makePropertyChangedNotify(this, "exceptionHandlerAdd", null, exceptionHandler);
	}
	
	public void removeExceptionHandler(ExceptionHandler exceptionHandler) {
		exceptionHandlers.remove(exceptionHandler);
		makePropertyChangedNotify(this, "exceptionHandlerRemove", exceptionHandler, null);
	}
	
	public ExceptionHandler[] getExceptionHandlers() {
		return (ExceptionHandler[])exceptionHandlers.toArray(new ExceptionHandler[exceptionHandlers.size()]);
	}
	
	public void addTransition(Transition transition) {
		if (transitions.contains(transition)) return;
		transitions.add(transition);
		makePropertyChangedNotify(this, "transitionAdd", null, transition);
	}
	
	public void removeTransition(Transition transition) {
		if (!(transitions.contains(transition))) return;
		transitions.remove(transition);
		makePropertyChangedNotify(this, "transitionRemove", transition, null);
	}
	
	public Transition[] getTransitions() {
		return (Transition[])transitions.toArray(new Transition[transitions.size()]);
	}
	
	public boolean isPossibleChildOf(NodeElementContainer nodeElementContainer) {
		return true;
	}
	
	public void initializeName(NodeElementContainer nodeElementContainer) {
		int runner = 1;
		String prefix = getNamePrefix();
		while (true) {
			String candidate = prefix + runner;
			if (nodeElementContainer.getNodeElementByName(candidate) == null) {
				setName(candidate);
				return;
			}
			runner ++;
		}
	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public spe.model.common.NodeElement getParent() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setParent(spe.model.common.NodeElement p) {
		// TODO Auto-generated method stub
		
	}
	
}
